<?php
/*
 * File rijschool.php for project Rijlesplanner.nl
 * Created on 4 mar 2008 at 21:30:23 by Ivar Pruijn
 */
class rijschool extends modules {
	// Properties:
	protected $name = "rijschool";
	protected $title = "Mijn rijschool";
	protected $subtitle = "";
	protected $moduleOptions = array ();
	private $errors = array ();

	// Methods:

	/**
	 * void __construct ()
	 * This is the constructor method for this class.
	 */
	public function __construct() {
		parent :: __construct();
		$this->logger = parent :: getLogger();
		$this->user = parent :: getUser();
		$this->template = parent :: getTemplate();
	}

	/**
	 * Object process()
	 * 
	 * Processes the template and current module
	 * 
	 * @param Smarty template The Smarty template to process (sets variables etc.)
	 * @return Smarty The template that has been processed
	 */
	function process(& $template = null) {
				
		$this->template = $template;
		
		parent::loadModule("ads",$template);
		
		
		if($this->user instanceof Visitor) {
			parent::loadModule("authentication",$template);
  		} else if($this->user instanceof Leerling) {
  			$this->template->assign('rijschool',array('naam'=>$this->user->getRijschoolNaam(),'id'=>$this->user->getRijschoolId()));
  		}
		if (!empty ($_POST)) {
			if (!$this->auth->accessTo($this->name,null,$_POST['action'])) {
				$this->actionAccessDenied();
			} else {
				switch ($_POST['action']) {
	  	    		default :
	  	    			break;
				}
			}
		} else if(!empty($_GET['option'])) {
			if (!$this->auth->accessTo($this->name,$_GET['option'])) {
				$this->optionAccessDenied();
			} else {
	  	  		switch ($_GET['option']) {
	  	    		default:
						$this->rijschoolPageContent();
						$this->template->assign('moduleTemplate', 'modules/rijschool/mijnrijschool_leerling.tpl');
	  	    			break;
	  	  		}
			}
		} else {
			parent::loadModule("reviews",$template);
			if($this->user instanceof Visitor) {
				$this->template->assign('moduleTemplate', 'common/option_accessdenied.tpl');
			} else if ($this->user instanceof Leerling) {
				$this->rijschoolPageContent();
				$this->template->assign('moduleTemplate', 'modules/rijschool/mijnrijschool_leerling.tpl');
			}
		}
	}
	
	function rijschoolPageContent() {
		$errors = array ();
		try {
			$result = $this->wsclient->WS_Rijlesplanner_GetContent(array (
				'RijschoolId' => $this->user->getRijschoolId(),
				'SessionId' => $_SESSION['SessionId'],
				'PageId' => 'mededelingen'
			));
			$this->logger->log("Mededelingen result: ".print_r($result,true),PEAR_LOG_DEBUG);
			$content = $result->Content;
			$this->template->assign('pageContent', $content);

			$result = $this->wsclient->WS_Rijschool_NAW(array (
				'RijschoolId' => $this->user->getRijschoolId(),
				'SessionId' => $_SESSION['SessionId']
			));
			$this->logger->log("Rijschool info result: ".print_r($result,true),PEAR_LOG_DEBUG);
			$rijschoolNAW = $result;
			$this->template->assign('rijschoolNAW', $rijschoolNAW);
		} catch (SoapFault $e) {
			$this->logger->log("SOAP Fault: (faultcode: {$e->faultcode} faultstring: {$e->faultstring})", PEAR_LOG_INFO);
			switch ($e->faultcode) {
				default :
					$errorcode = 65;
					$errormessage = 'Onbekende fout bij ophalen rijschool pagina content ("'.$e->faultstring.'")';
					break;
			}
			$errors[] = array (
				'code' => $errorcode,
				'message' => $errormessage
			);
			$this->setSubtitle($errormessage);
		}
		$this->template->assign('errors', $errors);
	}

	public function addError($message, $code) {
		$this->errors[] = array (
			'code' => $code,
			'message' => $message
		);
		$this->setSubtitle($message);
	}

	public function getTitle() {
		return $this->title;
	}

	public function getSubTitle() {
		return $this->subtitle;
	}

	public function setSubTitle($newVal = "") {
		if (!empty ($newVal)) {
			$this->subtitle = $newVal;
		}
	}

	/*
	 * string __toString()
	 * 
	 * Returns a String representation of this class.
	 * 
	 * @return string A representation of this class
	 */
	public function __toString() {
		return $this->title;
	}
}
?>